#include "CCmdInnerUserLogin.h"
#include "../mainctrl.h"
#include "../cmd/cmdapi.h"
#include "../cmd/dataprepare.h"
#include "commmsg.h"

CT_REGISTER_ASYNCCMD(ID_MAIN_INNER_USER_LOGIN, CCmdInnerUserLogin);

int CCmdInnerUserLogin::Do(void* pData)
{
    if (pData != NULL)
    {
        CT_ERROR(("inner cmd CCmdInnerUserLogin get data"));
        return AnsClients(RET_SYSTEM_FAIL);
    }

    if (pstMainCtrl == NULL)
    {
        CT_ERROR(("mainctrl point is null"));
        return AnsClients(RET_SYSTEM_FAIL);
    }

    _bIsCheckLogin = false;
    _uiGID = 0;

    return LogicDo(RET_OK);
}

int CCmdInnerUserLogin::LogicDo(int iRet)
{
    if (iRet != RET_OK)
    {
        return AnsClients(iRet);
    }

    int iIndex = 0;

    if (_bIsTencentLogin && !_bIsCheckLogin)
    {
        _bIsCheckLogin = true;

        //平台校验登录
        CCmdApi* pstCmd = (CCmdApi*)CT_CMDFACTORY->CreateCmd(ID_MAINAPI_TAPI);
        if (!pstCmd)
        {
            CT_ERROR(("create cmd error"));
            return AnsClients(RET_SYS_ERROR);
        }

        pstCmd->pstMainCtrl = CMainCtrl::m_pstMain;
        pstCmd->stReq._ucReqType = CReqApi::API_TENCENT;

        pstCmd->stReq._szTApiName.Set("/v3/user/is_login");
        pstCmd->stReq._stTApiKeyValue = _stTencentApiKeyValue;
        
        RegisterChild(pstCmd, true);
        return pstCmd->Do(NULL);
    }

    //检查用户是否存在
    iIndex = pstMainCtrl->m_stUserTokenToGID.HashFind(_szUserToken);
    if (iIndex >= 0)
    {
        CGidList& stGidList = pstMainCtrl->m_stUserTokenToGID[iIndex];

        for (int i = 0; i < stGidList._auiGID.Size(); ++i)
        {
            //返回列表
            iIndex = pstMainCtrl->m_stPlayerSnsCommInfoHash.HashFind(stGidList._auiGID[i]);
            if (iIndex < 0)
            {
                CT_WARNING(("gid %u not find", stGidList._auiGID[i]));
                continue;
            }
            CPlayerSnsCommInfo& stPlayerSnsCommInfo = pstMainCtrl->m_stPlayerSnsCommInfoHash[iIndex];

            if (stPlayerSnsCommInfo._sRoleName.GetDataLen() == 0)
            {
                continue;
            }

            MsgMultiAccountInfo* pstMsgMultiAccountInfo = stAns.add_multi_account();
            pstMsgMultiAccountInfo->set_gid(stPlayerSnsCommInfo._uiGID);
            pstMsgMultiAccountInfo->set_rolename(stPlayerSnsCommInfo._sRoleName());
            pstMsgMultiAccountInfo->set_career(stPlayerSnsCommInfo._ucCareer);
            pstMsgMultiAccountInfo->set_level(stPlayerSnsCommInfo._usLevel);
            pstMsgMultiAccountInfo->set_sex(stPlayerSnsCommInfo._ucIsMale);
        }
    }

    return AnsClients(RET_OK);
}

int CCmdInnerUserLogin::AnsClients(int iRet)
{
    pstMainCtrl->m_stEncodeBuf.Clear();

    if (RET_OK != iRet)
    {
        CT_ERROR(("CCmdLogin::LogicDo OPENID %s error %d", _szUserToken(), iRet));
    }

    stMsgHead.set_msgret(iRet);
    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);

    if (iRet != RET_OK)
    {
        pstMainCtrl->ReturnMsgHead(stMsgHead, stEnpHead);
        return Done(iRet);
    }

    stAns.set_gid(_uiGID);

    pstMainCtrl->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
    pstMainCtrl->EncodePBMsg(stAns, pstMainCtrl->m_stEncodeBuf);

    if (pstMainCtrl->AnswerToClient(stEnpHead, pstMainCtrl->m_stEncodeBuf, true))
    {
        CT_WARNING((""));
        return Done(RET_SYS_ERROR);
    }

    return Done(iRet);
}

